package cn.rongdao.jrkabao.ui.message;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.message.MessageBean;
import cn.rongdao.jrkabao.model.message.MessageModel;
import cn.rongdao.jrkabao.ui.message.adapter.MessageAdapter;
import cn.rongdao.jrkabao.ui.message.adapter.MessageViewHolder;
import cn.rongdao.jrkabao.ui.message.notice.NoticeMessageActivity;
import cn.rongdao.jrkabao.ui.message.system.SystemMessageActivity;

/**
 * Created by jack on 2017/8/16
 * @author jack
 */

public class MessageActivity extends BaseAutoFinishActivity implements FRBaseRecyclerAdapter.OnItemClickListener<MessageViewHolder> {

    public static void start(Context context) {
        FRStartActivity.start(context, MessageActivity.class);
    }

    @BindView(R.id.am_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.am_ptrframelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;

    private MessageAdapter mAdapter;

    @Override
    protected int layoutRes() {
        return R.layout.activity_message;
    }

    @Override
    protected void initView() {
        super.initView();
        initTitleBar();
        mAdapter = new MessageAdapter(this, this);
        ptrRecyclerViewUIComponent.setBackground(R.color.layout_bg);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(this));
        ptrRecyclerViewUIComponent.setAdapter(mAdapter);
        initListener();
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("我的消息");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initListener() {
        ptrRecyclerViewUIComponent.delayRefresh();
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                getMessageData();
            }
        });
    }

    private void getMessageData() {
        MessageModel.getInstance().execute(new FRBaseObserver<List<MessageBean>>(this) {
            @Override
            protected void onBaseNext(List<MessageBean> data) {
                mAdapter.setDataList(data);
                runUI();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                runUI();
            }
        });
    }

    private void runUI() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ptrRecyclerViewUIComponent.refreshComplete();
            }
        });
    }

    @Override
    public void onItemClick(MessageViewHolder messageViewHolder, int position) {
        String msgEn = mAdapter.getDataList().get(position).getMsg_en();
        if (FRString.isEmpty(msgEn)) {
            return;
        }
        MessageBean messageBean = mAdapter.getDataList().get(position);
        messageBean.setIs_read(0);
        mAdapter.updateItem(position, messageBean);
        if (msgEn.equalsIgnoreCase("system")) {
            SystemMessageActivity.start(thisActivity());
        } else if (msgEn.equalsIgnoreCase("advertisement")) {
            NoticeMessageActivity.start(thisActivity());
        }
    }
}
